package com.hdax.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdax.entity.Activity;
import com.hdax.service.ActivityService;
import com.hdax.vo.ActivityIdModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping("/findActivity")
    public List<Activity> findActivity() {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper();
        return activityService.list(queryWrapper);
    }

    @GetMapping(path = "list")
    public ModelAndView list(){
        return new ModelAndView("activity/list");
    }

    @RequestMapping(path = "/index")
    @ResponseBody
    public Map<String,Object> index(
            Activity name,
            @RequestParam("page") int page,
            @RequestParam("limit") int limit
    ){

        Map<String,Object> map=activityService.listByPage(page,limit,name);
        return map;

    }
    @GetMapping(path = "/add")
    public ModelAndView toAdd(){
        return new ModelAndView("activity/add");
    }

    @PostMapping(path = "/add")
    @ResponseBody
    public Map<String,Object> toAdd(Activity activity){
        Map<String,Object> map=new HashMap<>();
        activity.setCreateTime(LocalDateTime.now());
        activity.setActivityState(2);
        int flag = activityService.add(activity);
        map.put("msg",flag);
        return map;

    }


    @GetMapping(path = "/update/{id}")
    public ModelAndView update(@PathVariable("id") Integer id){
        ModelAndView modelAndView=new ModelAndView("activity/update");
        Activity activity=activityService.getById(id);
        modelAndView.addObject("activity",activity);
        return modelAndView;

    }


    @PostMapping(path = "/update")
    @ResponseBody
    public Map<String,Object> update(Activity activity){
        Map<String,Object> map=new HashMap<>();
        activity.setUpdateTime(LocalDateTime.now());
        int flag=activityService.update(activity);
        map.put("msg",flag);
        return map;
    }

    @PostMapping(path = "/del")
    @ResponseBody
    public Map<String,Object>delete(ActivityIdModel activityIdModel){
        Map<String,Object>map=new HashMap<>();
        activityService.btnDel(activityIdModel.getIds());
        map.put("msg","success");
        return map;
    }

    @PostMapping(path = "delete/{id}")
    @ResponseBody
    public Map<String,Object>delete(@PathVariable("id") Integer id){
        Map<String,Object>map=new HashMap<>();
        boolean flag=activityService.removeById(id);
        map.put("msg",flag);
        return map;
    }
}

